package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.ulits.DateUtils;
import com.itheima.ulits.POIUtils;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约控制层
 *
 * @author ：zengjie
 * @date ：2020/11/11
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 批量上传预约
     *
     * @param excelFile
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Result upload(MultipartFile excelFile) {
        try {
            List<String[]> listStr = POIUtils.readExcel(excelFile);
            List<OrderSetting> oslist = new ArrayList<>();
            if (listStr != null && listStr.size() > 0) {
                for (String[] strings : listStr) {
                    OrderSetting os = new OrderSetting();
                    os.setOrderDate(new Date(strings[0]));
                    os.setNumber(Integer.parseInt(strings[1]));
                    oslist.add(os);
                }
                orderSettingService.batchOrderSetting(oslist);
                return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
    }

    /**
     * 根据年月来获取日历组件需要的数据
     */
    @RequestMapping(value = "/findOSByYearMonth",method = RequestMethod.GET)
    public Result findOSByYearMonth(String yearMonth){
        try {
            List<Map> mapList = orderSettingService.findOSByYearMonth(yearMonth);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

}
